const mongoose = require("mongoose");
const Meta = mongoose.model("Meta");
const { initAcl } = require("../../config/acl");
const { log, Resp, validObjectId } = require("../../config/utils");
class MetaCtrl {
	// 权限列表
	async list(req, res, next) {
		const cond = {};
		const { group } = req.query;
		if(group) cond["group"] = group;
		try {	
			let metas = await Meta.find(cond).sort({_id: "DESC"});
			res.send(new Resp(1, "查询权限成功!", metas));
		} catch (err) {
			log("red", err.stack);
			next(new Error("查询权限出错!"));
		}
	}
	// 创建权限
	async create(req, res, next) {
		let { group, permission, roles, group_des, permission_des} = req.body;
		if(!group) return next(new Error("缺少参数: group!"));
		if(!permission) return next(new Error("缺少参数: permission!"));
		if(!group_des) return next(new Error("缺少参数: group_des!"));
		if(!permission_des) return next(new Error("缺少参数: permission_des!"));
		if(!roles) return next(new Error("缺少参数: roles!"));
		if(!(roles instanceof Array)) roles = [roles];
		try {
			let oldMeta = await Meta.findOne({ group, permission });
			if(oldMeta) return next(new Error("此权限已存在!"));
			let meta = await Meta.create({group, permission, roles, group_des, permission_des});
			await initAcl();
			res.send(new Resp(1, "创建权限成功!", meta));
		} catch (err) {
			log("red", err.stack);
			next(new Error("创建权限失败!"));
		}
	}
	// 修改权限
	async update(req, res, next) {
		let { _id, group, permission, roles, group_des, permission_des} = req.body;
		if(!_id) return next(new Error("缺少权限id!"));
		try {
			let meta = await Meta.findOne({ _id});
			if(!meta) return next(new Error("未找到此权限!"));
			if(group) meta.group = group;
			if(permission) meta.permission = permission;
			if(group_des) meta.group_des = group_des;
			if(permission_des) meta.permission_des = permission_des;
			if(roles) meta.roles = (roles instanceof Array) ? roles : [roles];
			let result = await meta.save();
			await initAcl();
			res.send(new Resp(1, "修改权限成功!", result));
		} catch (err) {
			log("red", err.stack);
			next(new Error("创建权限失败!"));
		}
	}
	// 删除权限
	async del(req, res, next) {
		let { ids } = req.params;
		if (!ids) return next(new Error("缺少权限id!"));
		ids.split(",");
		if(!(ids instanceof Array)) ids = [ids];
		if(!validObjectId(ids)) return next(new Error("权限id格式错误!"));
		try {
			let metas = await Meta.find({ _id: ids });
			if(metas.length === 0) return next(new Error("未找到任何权限!"));
			let unCheckMetas = {};
			ids.forEach(id => {
				unCheckMetas[id] = true;
			});
			metas.forEach(meta => {
				if(unCheckMetas[meta._id]) {
					delete unCheckMetas[meta._id];
				}
			});
			if(metas.length < ids.length) return next(new Error(`部分权限不存在(${Object.keys(unCheckMetas).join(",")})!`));
			let result = await Meta.deleteMany({_id: ids});
			if(result.ok === 1) {
				await initAcl();
				return res.send(new Resp(1, "删除权限成功!", null));
			}
		} catch (err) {
			log("red", err.stack);
			return next(new Error("删除权限出错!"));
		}
	}

}

module.exports = new MetaCtrl();